package controller

import (
	"github.com/gin-gonic/gin"
	"net/http"
	"strconv"
	"strings"
	"time"
)

type SaveFileController struct {
}

func (SaveFileController) Save(c *gin.Context) {
	file, _ := c.FormFile("file")
	//fmt.Printf("%#v", file.Filename)
	fileNameArr := strings.Split(file.Filename, ".")
	filename := fileNameArr[0] + strconv.Itoa(int(time.Now().UnixNano())) + "." + fileNameArr[1]
	if err := c.SaveUploadedFile(file, "static/"+filename); err != nil {
		c.AbortWithStatus(http.StatusBadRequest)
		return
	}

	c.JSON(200, gin.H{
		"code": 0,
		"body": filename,
	})
}
func (SaveFileController) DeleteImg(c *gin.Context) {
	c.PostForm("img")
	//file, _ := c.FormFile("file")
	//fmt.Printf("%#v", file.Filename)
	//fileNameArr := strings.Split(file.Filename, ".")
	//filename := fileNameArr[0] + strconv.Itoa(int(time.Now().UnixNano())) + "." + fileNameArr[1]
	//if err := c.SaveUploadedFile(file, "static/temp/"+filename); err != nil {
	//	c.AbortWithStatus(http.StatusBadRequest)
	//	return
	//}
	//
	//c.JSON(200, gin.H{
	//	"code": 0,
	//	"body": "temp/" + filename,
	//})
}
